package com.sunriz.motordrive.controller.photovoltaic;

import com.sunriz.motordrive.domain.DTO.BatchConfigDTO;
import com.sunriz.motordrive.domain.DTO.BatchReturnValueDTO;
import com.sunriz.motordrive.domain.DTO.Result;
import com.sunriz.motordrive.exception.CrcValidationException;
import com.sunriz.motordrive.mapper.BatchHcMapper;
import com.sunriz.motordrive.server.common.SerialPortServer;
import com.sunriz.motordrive.server.common.impl.BaseSerialImpl;
import com.sunriz.motordrive.server.photoVoltaic.BatchHcServer;
import com.sunriz.motordrive.utils.ArrayUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: HcsInfoDataController
 * Package: com.sunriz.motordrive.controller.photovoltaic.hc
 * Description:
 *  批量查询配置
 * @Author BuTang
 * @Create 2024/9/13 11:30
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/photoVoltaic/batchConfig")
public class BatchHcController {

    @Autowired
    BatchHcServer batchHcServer;

    @PostMapping("/config/add")
    public Result addConfig(@RequestBody() BatchConfigDTO batchConfigDTO ){
        batchHcServer.addConfig(batchConfigDTO);
        log.info("新增配置成功{}",batchConfigDTO);
        return Result.success();
    }

    @GetMapping("/config/getList")
    public Result getList(@RequestParam int configId){
        List<BatchConfigDTO> list = batchHcServer.getList(configId);
        return Result.success(list);
    }

    @GetMapping("/config/getConfigList")
    public Result getConfigList(){
        List list = batchHcServer.getConfigList();
        return Result.success(list);
    }

    @PutMapping("/config/updateConfig")
    public Result updateConfig(@RequestBody BatchConfigDTO batchConfigDTO){
        batchHcServer.updateConfig(batchConfigDTO);
        return Result.success();
    }


    @Autowired
    SerialPortServer serialPortServer;
    @Autowired
    BatchHcMapper batchHcMapper;
    @Autowired
    BaseSerialImpl serialPortImpl;
    private static int RATE_115200 = 115200;
    private static int RATE_921600 = 921600;
    private static String port = "com1";
    private static String slaveId = "17";


    @GetMapping("/enable")
    public Result enableTest (){
        serialPortImpl.enableSerialPort("COM6",RATE_115200);
        return Result.success();
    }

    @GetMapping("/test1")
    public Result test1() throws InterruptedException {
        int configId = 1;

        // 存储
        List<BatchReturnValueDTO> returnList = new ArrayList();
        BatchReturnValueDTO v1 = new BatchReturnValueDTO();
        BatchReturnValueDTO v2 = new BatchReturnValueDTO();

        // 获取配置信息
        List<BatchConfigDTO> list = batchHcMapper.getList(configId);
        List list32 = new ArrayList();
        for (int i = 0; i < list.size(); i++) {

            // 配置信息
            BatchConfigDTO configDTO = list.get(i);
            String byteSize = configDTO.byteSize;
            String address = configDTO.address;
            String slaveId = configDTO.getSlaveId();

            Integer size = Integer.valueOf(byteSize);
            if (size.equals(2)) {
                list32.add(address);
            }
            BaseSerialImpl.list32 = list32;



            // todo model还未修改，以后可能要添加到存储数据库中
//            serialPortImpl.sendModbus(ArrayUtils.fillIn0304Msg(Integer.valueOf(slaveId),3, Integer.parseInt(address), Integer.parseInt(byteSize)));
//            Thread.sleep(20);
//            int returnValue1 = BaseSerialPortImpl.RETURN_VALUE;
//
//            System.out.println("value :"+ returnValue1);
//            BaseSerialPortImpl.RETURN_VALUE = 0;


        }


        // value1
        serialPortImpl.sendModbus(ArrayUtils.fillIn0304Msg(Integer.valueOf(slaveId),3,9,1));
        Thread.sleep(20);
        String returnValue1 = BaseSerialImpl.RETURN_VALUE;
        BaseSerialImpl.RETURN_VALUE = String.valueOf(0);

        v1.setName("name:1");
        v1.setAddress("address:9");
        v1.setSlaveId("slaveId:17");
        v1.setValue(String.valueOf(returnValue1));

        // value2
        serialPortImpl.sendModbus(ArrayUtils.fillIn0304Msg(Integer.valueOf(slaveId),3,10,1));
        Thread.sleep(20);
        String returnValue2 = BaseSerialImpl.RETURN_VALUE;
        BaseSerialImpl.RETURN_VALUE = String.valueOf(0);

        v2.setName("name:2");
        v2.setAddress("address:9");
        v2.setSlaveId("slaveId:17");
        v2.setValue(String.valueOf(returnValue2));



        // 打印
        log.info("value1:{},value2:{}",returnValue1,returnValue2);

        returnList.add(v1);
        returnList.add(v2);
        System.out.println(returnList);


        return Result.success();
    }

    @GetMapping("/test2")
    public void test2() {
        List<BatchConfigDTO> list = batchHcMapper.getList(1);
        System.out.println(1);
        throw  new CrcValidationException("CRC校验失败，报文不处理");
    }



}
